import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import store from "@/store";
import Message from "@/components/utils/message";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
    redirect: "/container",
    children: [
      {
        path: "/container",
        name: "container",
        component: () => import("../components/ContAiner.vue"),
      },
      {
        path: "/cate",
        name: "cate",
        component: () => import("../components/cate/CateGoryed.vue"),
      },
      {
        path: "/subCate",
        name: "subCate",
        component: () => import("../components/subCate/SubCateView.vue"),
      },
      {
        path: "/detail",
        name: "detail",
        component: () => import("../components/DetailView.vue"),
      },
      {
        path: "/cart",
        name: "cart",
        component: () => import("../components/cart/GoodsCart.vue"),
      },
      {
        path: "/member/order",
        name: "order",
        component: () => import("../components/order/GoodsOrder.vue"),
      },
      {
        path: "/member/pay",
        name: "pay",
        component: () => import("../components/pay/PayView.vue"),
      },
      {
        path: "/member/member",
        name: "member",
        component: () => import("../components/member/MemberView.vue"),
        children:[
          {
            path: "/member/myOrder",
            name: "myOrder",
            component: () => import("../components/member/MemberOrder.vue"),
          },
          {
            path: "/member/center",
            name: "center",
            component: () => import("../components/member/MemberCenter.vue"),
          },
        ]
      },
    ],
  },
  {
    path: "/about",
    name: "about",
    component: () => import("../views/AboutView.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/AppLogin.vue"),
  },
  {
    path: "/login/callback",
    name: "LoginCallback",
    component: () => import("../components/login/LoginCallback.vue"),
  },
  {
    path: "/nopage",
    name: "nopage",
    component: () => import("../views/NoPage.vue"),
  },
  {
    path: "/lunbo",
    name: "lunbo",
    component: () => import("../components/index/LunBo.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  routes,
  /* 路由跳转后滚动条在顶部 */
  scrollBehavior() {
    return { x: 0, y: 0 };
  },
});

router.beforeEach((to, from, next) => {
  if (to.fullPath.startsWith("/member")) {
    //判断是否登录
    if (store.state.user.userinfo && store.state.user.userinfo.token) {
      next();
    } else {
      //没登录，提示回到登录页
      Message({ text: "请先登录", type: "error" });
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
